package com.bite.friend.service.exam.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.bite.common.core.domain.TableDataInfo;
import com.bite.common.core.enums.ResultCode;
import com.bite.common.security.exception.ServiceException;
import com.bite.friend.domain.exam.dto.ExamQueryDTO;
import com.bite.friend.domain.exam.vo.ExamVO;
import com.bite.friend.manager.ExamCacheManager;
import com.bite.friend.manager.QuestionCacheManager;
import com.bite.friend.mapper.exam.ExamMapper;
import com.bite.friend.service.exam.IExamService;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private ExamCacheManager examCacheManager;

    @Autowired
    private QuestionCacheManager questionCacheManager;

    @Override
    public List<ExamVO> list(ExamQueryDTO examQueryDTO) {
        return examMapper.list(examQueryDTO);
    }

    @Override
    public TableDataInfo redisList(ExamQueryDTO examQueryDTO) {
        // 从Redis中获取
        Long size = examCacheManager.getListSize(examQueryDTO.getType());
        if (size == null || size <= 0) {
            // 从数据库获取并刷新缓存
//            List<ExamVO> list = examMapper.list(examQueryDTO);
            examCacheManager.refreshCache(examQueryDTO.getType());
        }
        size = examCacheManager.getListSize(examQueryDTO.getType());
        List<ExamVO> examVOList = examCacheManager.getExamVOList(examQueryDTO);
        if (CollectionUtil.isEmpty(examVOList)) {
            return TableDataInfo.empty();
        }
        assembleExamVoList(examVOList);
        return TableDataInfo.success(examVOList, size);
    }

    @Override
    public String getExamFirstQuestion(Long examId) {
        Long questionId = questionCacheManager.getExamFirstQuestion(examId);
        if (questionId == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        return questionId.toString();
    }

    @Override
    public String preQuestion(Long examId, Long questionId) {
        Long preQuestionId = questionCacheManager.preQuestion(questionId, examId);
        if (preQuestionId == null) return null;
        return preQuestionId.toString();
    }

    @Override
    public String nextQuestion(Long examId, Long questionId) {
        Long nextQuestionId = questionCacheManager.nextQuestion(questionId, examId);
        if (nextQuestionId == null) return null;
        return nextQuestionId.toString();
    }


    // 判断当前用户是否已经参赛
    private void assembleExamVoList(List<ExamVO> examVOList) {
        Set<Long> userEnterExam = new HashSet<>(examCacheManager.getUserEnterExam());
        examVOList.forEach(examVO -> {
            examVO.setEnter(userEnterExam.contains(Long.parseLong(examVO.getExamId())));
        });
    }

}




